07. Writing a Plugin

Writing a Plugin

Writing a Plugin

Simple steps to interact with a World in Gazebo through a World plugin

1- Create a directory for scripts inside “myrobot” to store a hello.cpp file

$ cd /home/workspace/myrobot
$ mkdir script
$ cd script
$ gedit hello.cpp

Inside hello.cpp , include this code:

#include <gazebo/gazebo.hh>

namespace gazebo
{
  class WorldPluginMyRobot : public WorldPlugin
  {
    public: WorldPluginMyRobot() : WorldPlugin()
            {
              printf("Hello World!\n");
            }

    public: void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)
            {
            }
  };
  GZ_REGISTER_WORLD_PLUGIN(WorldPluginMyRobot)
}

2- Create a CMakeLists.txt file

$ cd /home/workspace/myrobot
$ gedit CMakeLists.txt

Inside, CMakeLists.txt , include the following:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

find_package(gazebo REQUIRED)
include_directories(${GAZEBO_INCLUDE_DIRS})
link_directories(${GAZEBO_LIBRARY_DIRS})
list(APPEND CMAKE_CXX_FLAGS "${GAZEBO_CXX_FLAGS}")

add_library(hello SHARED script/hello.cpp)
target_link_libraries(hello ${GAZEBO_LIBRARIES})

3- Create a build directory and compile the code

$ cd /home/workspace/myrobot
$ mkdir build
$ cd build/
$ cmake ../
$ make # You might get errors if your system is not up to date!
$ export GAZEBO_PLUGIN_PATH=${GAZEBO_PLUGIN_PATH}:/home/workspace/myrobot/build

4- Open your world file and attach the plugin to it

$ cd /home/workspace/myrobot/world/
$ gedit myworld

Copy this code
```html

and paste it under 

html
```

5- Launch the world file in Gazebo to load both the world and the plugin

$ cd /home/workspace/myrobot/world/
$ gazebo myworld

6- Visualize the output

  • A Hello World! message is printed in the terminal. This message interacts with the Gazebo World that includes your two-wheeled robot.

Troubleshooting

In case your plugins failed to load, you'll have to check and troubleshoot your error. The best way to troubleshoot errors with Gazebo is to launch it with the verbose as such:

$ gazebo myworld --verbose

GitHub Repo

I hope you followed along with these instructions, built your first model in Gazebo, included in an empty world, and interacted with the world through a World Plugin. You can always clone this lab from GitHub by clicking here . Follow the instructions in the Readme file to launch the world.